package com.ruoyi.eamp.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.eamp.domain.EampReLogs;
import com.ruoyi.eamp.domain.EampReMan;
import com.ruoyi.eamp.domain.EampReMylog;
import com.ruoyi.eamp.domain.vo.BingVO;
import com.ruoyi.eamp.domain.vo.EchartZhuVO;
import com.ruoyi.eamp.mapper.EampReManMapper;
import com.ruoyi.eamp.service.IEampReLogsService;
import com.ruoyi.eamp.service.IEampReManService;
import com.ruoyi.eamp.service.IEampReMylogService;
import com.ruoyi.eamp.service.IEampSrManService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 参试设备表Service业务层处理
 * 
 * @author ruoyi
 * @date 2024-08-08
 */
@Service
public class EampReManServiceImpl implements IEampReManService 
{
    @Autowired
    private EampReManMapper eampReManMapper;

    @Autowired
    private IEampSrManService eampSrManService;

    @Autowired
    private IEampReMylogService eampReMylogService;

    @Autowired
    private IEampReLogsService eampReLogsService;

    /**
     * 查询参试设备表
     * 
     * @param reId 参试设备表主键
     * @return 参试设备表
     */
    @Override
    public EampReMan selectEampReManByReId(Long reId)
    {
        return eampReManMapper.selectEampReManByReId(reId);
    }

    /**
     * 查询参试设备表列表
     * 
     * @param eampReMan 参试设备表
     * @return 参试设备表
     */
    @Override
    public List<EampReMan> selectEampReManList(EampReMan eampReMan)
    {
        return eampReManMapper.selectEampReManList(eampReMan);
    }

    /**
     * 新增参试设备表
     * 
     * @param eampReMan 参试设备表
     * @return 结果
     */
    @Override
    public int insertEampReMan(EampReMan eampReMan)
    {
        int result = 0;
        Long srId = eampReMan.getReSrId();
        if(eampSrManService.selectEampSrManBySrId(srId).getSrCurrentSpecifications() != 0){
            // 调用插入方法并获取返回结果
            result = eampReManMapper.insertEampReMan(eampReMan);
            // 如果插入成功
            if (result == 1) {
                // 检查 srId 是否有效
                if (srId != null) {
                    // 获取当前规格并判断是否需要更新
                        eampSrManService.updateAddEampSrManById(srId);
                }
            }
        }
        return result;
    }

    /**
     * 修改参试设备表
     * 
     * @param eampReMan 参试设备表
     * @return 结果
     */
    @Override
    public int updateEampReMan(EampReMan eampReMan)
    {
        return eampReManMapper.updateEampReMan(eampReMan);
    }

    /**
     * 批量删除参试设备表
     * 
     * @param reIds 需要删除的参试设备表主键
     * @return 结果
     */
    @Override
    public int deleteEampReManByReIds(Long[] reIds)
    {
        int result = eampReManMapper.deleteEampReManByReIds(reIds);
        if(result != 0){
            for (int i = 0; i < reIds.length; i++){
                Long reSrId = eampReManMapper.selectEampReManByReId(reIds[i]).getReSrId();
                eampSrManService.updateRemoveEampSrManById(reSrId);
            }
        }
        return result;
    }

    /**
     * 删除参试设备表信息
     * 
     * @param reId 参试设备表主键
     * @return 结果
     */
    @Override
    public int deleteEampReManByReId(Long reId)
    {
        int result = eampReManMapper.deleteEampReManByReId(reId);
        if(result != 0 ){
            Long reSrId = eampReManMapper.selectEampReManByReId(reId).getReSrId();
            eampSrManService.updateRemoveEampSrManById(reSrId);
        }
        return result;
    }

    @Override
    public int updateEampReManById(Long id) {
        return eampReManMapper.updateEampReManById(id);
    }

    @Override
    public int updateResult(Long id, Long reExResults) {
        return eampReManMapper.updateResult(id,reExResults);
    }

    /*
    * 出库的时候生成一条记录，添加到我的使用，和使用记录里
    * */
    @Override
    public int updateStockeOutEampReManById(Long id) {
        int result = eampReManMapper.updateStockeOutEampReManById(id);
        if(result != 0){
            // 当前登录用户
            Long userId = SecurityUtils.getUserId();
            // 操作时间
            LocalDateTime localDate = LocalDateTime.now();
            // 操作方法
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            // 获取当前方法名（通常在调用栈的索引为2处）
            String methodName = stackTrace[2].getMethodName();
            System.out.println("当前方法名: " + methodName);
            EampReMylog eampReMylog = new EampReMylog(null,userId,localDate,methodName,id,1L);
            eampReMylogService.insertEampReMylog(eampReMylog);


            EampReLogs eampReLogs = new EampReLogs(null,userId,localDate,methodName,id,1L);
            eampReLogsService.insertEampReLogs(eampReLogs);
        }
        return result;
    }

    @Override
    public int updateStockeInEampReManById(Long id) {

        int result = eampReManMapper.updateStockeInEampReManById(id);
        if(result != 0){
            // 当前登录用户
            Long userId = SecurityUtils.getUserId();
            // 操作时间
            LocalDateTime localDate = LocalDateTime.now();
            // 操作方法
            StackTraceElement[] stackTrace = Thread.currentThread().getStackTrace();
            // 获取当前方法名（通常在调用栈的索引为2处）
            String methodName = stackTrace[2].getMethodName();
            System.out.println("当前方法名: " + methodName);
            EampReMylog eampReMylog = new EampReMylog(null,userId,localDate,methodName,id,1L);
            eampReMylogService.insertEampReMylog(eampReMylog);

            EampReLogs eampReLogs = new EampReLogs(null,userId,localDate,methodName,id,1L);
            eampReLogsService.insertEampReLogs(eampReLogs);
        }
        return result;
    }

    @Override
    public   List<List<BingVO>> selectEampReManEcharList() {
        List<EchartZhuVO> list = eampReManMapper.selectEampReManEcharList();
        List<List<BingVO>> listList = new ArrayList<>();
        List<BingVO>  no1=new ArrayList<>();
        List<BingVO>  no2=new ArrayList<>();
        List<BingVO>  no3=new ArrayList<>();
        int a=0,b=0,c=0;
        for (EchartZhuVO echartZhuVO : list) {
            for (EampReMan eampReMan : echartZhuVO.getEampReManList()) {
                if (eampReMan.getReState()==0){
                    a+=1;
                }else if(eampReMan.getReState()==1){
                    b+=1;
                }else if(eampReMan.getReState()==2){
                    c+=1;
                }
            }
            BingVO v1=new BingVO("未试验");
            v1.setValue(a);
            no1.add(v1);
            BingVO v2=new BingVO("试验中");
            v2.setValue(b);
            no2.add(v2);
            BingVO v3=new BingVO("已试验");
            v3.setValue(c);
            no3.add(v3);
            a=b=c=0;
        }
        listList.add(no1);
        listList.add(no2);
        listList.add(no3);
        return listList;
    }

    @Override
    public List<String> selectSrName() {
        return eampReManMapper.selectSrName();
    }

    @Override
    public int getMax() {
        return eampReManMapper.getMax();
    }

}
